﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Logging;
using System.Diagnostics;
namespace DbFramework.Utility
{
    /// <summary>
    /// 日志类
    /// </summary>
    public class Logging
    {
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="logType">日志类型</param>     
        /// <param name="eventType">事件类型</param>
        /// <param name="message">错误消息</param>
        /// <param name="priority">优先级</param>
        public static void SaveLog(ELogType logType, String message, EPriority priority, TraceEventType eventType)
        {
            LogEntry entry = new LogEntry();
            entry.Message = message;
            entry.Priority = (int)priority;
            entry.MachineName = System.Net.Dns.GetHostName();
            entry.Severity = eventType;
            entry.TimeStamp = DateTime.Now;
            entry.Title = TextAttribute.GetEnumTextVal((int)logType, typeof(ELogType));
            entry.Categories.Add(TextAttribute.GetEnumTextVal((int)logType, typeof(ELogType)));
            entry.EventId = (int)logType;
            entry.AppDomainName = AppDomain.CurrentDomain.FriendlyName;
            try
            {
                Logger.Write(entry);
            }
            catch { }
        }
        /// <summary>
        /// 记录日志
        /// </summary>
        /// <param name="logType">日志类型</param>
        /// <param name="exp">异常对象</param>
        public static void SaveLog(ELogType logType, Exception exp, string extendMessage)
        {
            LogEntry entry = new LogEntry();
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("");
            sb.AppendLine("TimeStamp:" + DateTime.Now.ToString());
            sb.AppendLine("Exception Error Message:" + exp.Message);
            sb.AppendLine("Exception Source:" + exp.Source);
            sb.AppendLine("Exception TargetSite:" + exp.TargetSite.Name);
            if (exp.InnerException != null)
            {
                sb.AppendLine("InnerException Message:" + exp.InnerException.Message);
                sb.AppendLine("InnerException Source:" + exp.InnerException.Source);
            }
            if (extendMessage != string.Empty)
                sb.AppendLine("ExtendMessage:" + extendMessage);

            sb.AppendLine("");
            entry.Message = sb.ToString(); ;
            entry.Priority = (int)EPriority.High;
            entry.MachineName = System.Net.Dns.GetHostName();
            entry.Severity = TraceEventType.Error;
            entry.TimeStamp = DateTime.Now;
            entry.Title = TextAttribute.GetEnumTextVal((int)logType, typeof(ELogType));
            entry.Categories.Add(TextAttribute.GetEnumTextVal((int)logType, typeof(ELogType)));
            entry.EventId = (int)logType;
            entry.AppDomainName = AppDomain.CurrentDomain.FriendlyName;
            try
            {
                Logger.Write(entry.Message);
            }
            catch { }
        }


        public static void SaveLog(ELogType logType, Exception exp)
        {
            SaveLog(logType, exp, string.Empty);
        }
    }


}
